<%@page import="java.util.ArrayList"%>
<%@page import="models.OrderProductModel"%>
<%@page import="models.ProductModel"%>
<%@page import="java.util.List"%>
<%@page import="data.DealData"%>
<%@page import="data.GeoData"%>
<%@page import="java.util.TreeMap"%>
<%@page import="models.UserModel"%>
<%@page import="models.OrderModel"%>
<%@page import="java.util.Calendar"%>
<%@page import="models.GeoModel"%>
<%@page import="data.GeoData"%>
<%@page import="java.util.Locale"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="models.DealModel"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%	
	HttpSession ses = request.getSession();	
	UserModel user = (UserModel) ses.getAttribute("user_logged_in");
	OrderModel order = (OrderModel) ses.getAttribute("user_order");
	if(user==null){
		user = new UserModel();
	}
	TreeMap check = new TreeMap();
	if(request.getAttribute("check")!=null){
		check = (TreeMap) request.getAttribute("check");
	}	
	GeoData geoData = new GeoData();	
	String validStep1 = request.getAttribute("validStep1").toString();
	String validStep2 = request.getAttribute("validStep2").toString();
	String validStep3 = request.getAttribute("validStep3").toString();	
	String city =(String)request.getAttribute("city");
	if(city==null){
		city = "ha-noi";
	}
%>
<jsp:include page="/layouts/header.jsp">
	<jsp:param name="language" value="1" />	
</jsp:include>	 
<link rel="stylesheet" type="text/css" href="/css/ui-lightness/jquery-ui-1.8.18.custom.css" />
<script type="text/javascript" src="/js/jquery-ui-1.8.18.custom.min.js"></script>
<script type="text/javascript" src="/js/form-user.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function () {
	loadCity("<%=user.getCity() %>");
	loadDistrict("<%=user.getCity() %>", "<%=user.getDistrict() %>");
	loadWard("<%=user.getDistrict() %>", "<%=user.getWard()%>");
});
//-->
</script>
        <div id="content_inner">
            	<div id="productDetailInfo" class="clearafter">
                	<div class="left">                    	
                    	<div id="paymentPage">
							<h1>Thanh toán</h1>							
                            <div class="checkoutSteps">
                            	<div class="step-1 <%=(validStep1!=null&&validStep1.equals("1"))?"valid":"active" %>">
                                	<h2>1. Đăng nhập</h2>
                                    <div class="stepContent">
                                    <%
                                    	if(validStep1!=null&&validStep1.equals("3")){
                                    %>
                                    	<div class="notLogged clearafter">
                                            <div class="left">
                                                <div class="loginPopup">
                                                    <h2>Đăng nhập tài khoản Hotdeal</h2>
                                                    <form method="post" action="/thanh-toan">
                                                    <input type="hidden" name="action" value="login">
                                                        <div>
                                                            <label class="<%=(check.get("username") != null)?"error":""%>">
                                                                Email:
                                                                <input type="text" name="username" />
                                                                <% if (check.get("username") != null) { %>
			                                                    	<div class="triangleUp"></div>
	                                                                <div class="errorMessage">
	                                                                   <%=check.get("username")%>
	                                                                </div>
																<% } %>                                                                  
                                                            </label>
                                                        </div>
                                                        <div>
                                                            <label class="<%=(check.get("password") != null)?"error":""%>">
                                                                Mật khẩu:
                                                                <input type="password" name="password"/>
                                                                <% if (check.get("password") != null) { %>
			                                                    	<div class="triangleUp"></div>
                                                                	<div class="errorMessage">
                                                                		 <%=check.get("password")%>
                                                                	</div>
																<% } %>                                                                    
                                                            </label>
                                                        </div>
                                                        <div>
                                                            <label>
                                                                <input type="checkbox" />
                                                                Nhớ trạng thái đăng nhập
                                                            </label>                                                	
                                                        </div>
                                                        <div class="clearafter">
                                                            <button type="submit" class="left button blue">Đăng nhập</button>                                                            
                                                        </div>                                                        
                                                    </form>
                                                </div>
                                            </div>
                                            <div class="right">
                                                <h2>Tạo tài khoản mới </h2>
                                                <p>Bạn chưa có tài khoản Hotdeal? Hãy nhấp vào đây để đăng ký tài khoản mới. Bạn sẽ sử dụng tài khoản này để thực hiện các giao dịch, quản lý đơn hàng và theo dõi đơn hàng của bạn trên website của Hotdeal. </p>
                                                <div class="content-center"><a href="/register" class="button blue">Đăng ký</a></div>
                                            </div>
                                        </div>
                                        <%
                                    	}else if(validStep1!=null&&validStep1.equals("2")){
                                        %>
                                        <div class="logged clearafter">
                                        	<div class="left">
                                        		<div class="clearafter infoValidated">
                                                	<div class="left first">Thông tin đăng nhập:</div>
                                                    <div class="left last">
                                                    	<strong><%=user.getName()%></strong>
                                                    </div>
                                                </div>
                                                <form method="post" action="/thanh-toan">
                                                <input type="hidden" name="action" value="changeInfor"/>
                                                    <div>
                                                        <label class="<%=(check.get("username") != null)?"error":""%>">
                                                            Email: <input type="text" name="username" value="<%=user.getUsername()%>"/>
                                                            <% if (check.get("username") != null) { %>
		                                                    	<div class="triangleUp"></div>
                                                               	<div class="errorMessage">
                                                               		 <%=check.get("username")%>
                                                               	</div>
															<% } %>                                                              
                                                        </label>
                                                    </div>
                                                    <div>
                                                        <label>
                                                            Giới tính:
                                                            <select name="gender">
                                                                <option>--</option>
                                                                <option value="0" <%=user.getGender()==0?"selected":""%>>Nam</option>
                                                                <option value="1" <%=user.getGender()==1?"selected":""%>>Nữ</option>
                                                            </select>
                                                        </label>
                                                    </div>
                                                    <div><a href="/thanh-toan?act=doi-tai-khoan" class="otherAcc">Đăng nhập với tài khoản khác</a></div>
                                                    <div class="content-center">
                                                     	<button type="submit" class="button blue">Tiếp tục</button>                                                    	
                                                    </div>
                                                </form>
                                            </div>
                                        </div>    
                                        <%
                                    	}
                                        %>                                    
                                    </div>
                                                                      
                                    <%
										if(validStep1!=null&&validStep1.equals("1")&&!user.getUsername().equals("")){
                                    %>
                                    <div class="clearafter infoValidated">
                                        <div class="left first">Thông tin đăng nhập:</div>
                                        <div class="left last">
                                            <strong><%=user.getName() %></strong>
                                            <br />
                                            <span><%=user.getUsername() %></span>
                                        </div>
                                        <div class="clearbreak"></div>
                                        <div class="changeInfo"><a href="/thanh-toan?act=doi-thong-tin" class="changeInfo">Thay đổi</a></div>
                                    </div>
                                    <%
                                   		}
                                    %>
                                </div>
                               
                                                                                                                            
                                <%
                                	if(validStep1!=null&&validStep1.equals("1")){
                                %>
                                <div class="step-2 <%=(validStep2!=null&&validStep2.equals("1"))?"valid":"active" %>">
                                	<h2>2. Địa chỉ giao hàng</h2>
                                	<%
                                		if(validStep2!=null&&validStep2.equals("3")){
                                	%>
                                   	<div class="stepContent">
                                    	<form method="post" action="/thanh-toan">      
                                    	<input type="hidden" value="setOrder" name="action">                                  	
                                            <label class="clearafter <%=(check.get("shipName") != null)?"error":""%>">
                                                <span class="left required">Họ tên:</span>
                                                <div class="left">
                                                    <input type="text" name="shipName" value="<%=user.getName()%>"/>                                                   
                                                    <% if (check.get("shipName") != null) { %>
                                                    	<div class="triangleUp"></div>
														<div class="errorMessage">
	                                                        <%=check.get("shipName")%>
	                                                    </div>  
													<% } %>                                                    
                                                </div>
                                            </label>
                                            <label class="clearafter <%=check.get("city") != null ? "error":""%>">
			                                	<span class="left">Tỉnh/Thành phố:</span>
			                                    <div class="left">
			                                        <select name="city">
			                                            <option value="">Tỉnh/Thành phố</option>
			                                        </select>
			                                        <% if (check.get("city") != null) { %>
			                                        <div class="triangleUp"></div>
			                                        <div class="errorMessage">			                                        	
															<%=check.get("city")%>			                                        
			                                        </div>
			                                        <% } %> 
			                                    </div>
			                                </label>
			                                <label class="clearafter <%=check.get("district") != null ? "error":""%>">
			                                	<span class="left">Quận/Huyện: </span>
			                                    <div class="left">
			                                        <select name="district">
			                                            <option value="">Quận/Huyện</option>
			                                        </select>
			                                        <% if (check.get("district") != null) { %>
			                                        <div class="triangleUp"></div>
			                                        <div class="errorMessage">			                                        	
															<%=check.get("district")%>			                                        
			                                        </div>
			                                        <% } %> 
			                                    </div>
			                                </label>
			                                <label class="clearafter  <%=check.get("ward") != null ? "error":""%>">
			                                	<span class="left">Phường/Xã: </span>
			                                    <div class="left">
			                                        <select name="ward">
			                                            <option value="">Phường/Xã</option>
			                                        </select>
			                                       	<% if (check.get("ward") != null) { %>
				                                        <div class="triangleUp"></div>
				                                        <div class="errorMessage">			                                        	
																<%=check.get("ward")%>			                                        
				                                        </div>
			                                        <% } %> 
			                                    </div>
			                                </label>                                
                                            <label class="clearafter <%=(check.get("address") != null)?"error":""%>">
                                                <span class="left required">Số nhà - Ngõ - Ngách:</span>
                                                <div class="left">
                                                    <input type="text" name="address" value="<%=user.getAddress()%>"/> 
                                                    <% if (check.get("address") != null) { %>
                                                    	<div class="triangleUp"></div>
	                                                    <div class="errorMessage">
	                                                        <%=check.get("address")%>	
	                                                    </div>
													<% } %>                                                       
                                                </div>
                                            </label>
                                            <label class="clearafter <%=(check.get("street") != null)?"error":""%>">
                                                <span class="left required">Đường:</span>
                                                <div class="left">
                                                    <input type="text" name="street" value="<%=user.getStreet()%>"/>
                                                    <% if (check.get("street") != null) { %>
                                                    	<div class="triangleUp"></div>
	                                                    <div class="errorMessage">
	                                                         <%=check.get("street")%>	
	                                                    </div>
													<% } %>                                                      
                                                </div>
                                            </label> 
                                            <label class="clearafter">
                                                <span class="left">Lầu:</span>
                                                <div class="left">
                                                    <input type="text" name="floor" value="<%=user.getFloor()%>"/> 
                                                </div>
                                            </label> 
                                            <label class="clearafter <%=(check.get("phone") != null)?"error":""%>">
                                                <span class="left required">Điện thoại:</span>
                                                <div class="left">
                                                    <input type="text" name="phone" value="<%=user.getPhone()%>"/>
                                                    <% if (check.get("phone") != null) { %>
                                                    	<div class="triangleUp"></div>
	                                                    <div class="errorMessage">
	                                                        <%=check.get("phone") %>
	                                                    </div>
													<% } %>                                                     
                                                </div>
                                            </label> 
                                            <div class="content-center">
                                                <button type="submit" class="button blue">Tiếp tục</button>
                                            </div>                                            
                                        </form>
                                    </div>
                                    <%
                                		}else if(validStep2!=null&&validStep2.equals("1")){                     		                                		
                                    %>
                                    <div class="clearafter infoValidated">
                                        <div class="left first">Địa chỉ giao hàng:</div>
                                        <div class="left last">
                                            <strong><%=user.getName() %></strong>
                                            <br />
                                            <span><%=user.getAddress() %>, <span/><span><%=user.getStreet() %></span>
                                            <br />   
                                            <%
                                            	GeoModel ward = geoData.getGeo(user.getWard());
                                            	GeoModel district = geoData.getGeo(user.getDistrict());
                                            	GeoModel userCity = geoData.getGeo(user.getCity());
                                            %>                                         
                                            <%=(ward!=null)?ward.getName()+", ":""%><%=(district!=null)?district.getName()+", ":""%><%=(userCity!=null)?userCity.getName()+".":""%> 
                                        </div>
                                        <div class="clearbreak"></div>
                                        <div class="changeInfo"><a href="/thanh-toan?act=doi-thong-tin-order" class="changeInfo">Thay đổi</a></div>
                                    </div>                                    
                                    <%
                                		}%>                                		
                                		</div>
									<%
                                	}else{
                                		%>
                             	<div class="step-2">
                               		<h2>2. Địa chỉ thanh toán</h3>
                                </div>
                                		<%
                                	}
                                    %> 
                                <%
                                	if(validStep1!=null&&validStep1.equals("1")&&validStep2!=null&&validStep2.equals("1")){
                                %> 	                               
	                                <div class="step-3 <%=(validStep3!=null&&validStep3.equals("1"))?"valid":"active" %>">
	                                	<h2>3. Thanh toán và vận chuyển</h3>
	                                	<%
	                                		if(validStep3!=null&&validStep3.equals("3")){
	                                	%>
	                                    <div class="stepContent">
	                                    <form method="post" action="/thanh-toan" class="clearafter">
	                                    	<div class="paymentsMethod">	                                        	
	                                        	<input type="hidden" name="action" value="paymentType"/>
	                                                <div class="left">
	                                                    <h2>Chọn hình thức thanh toán</h2>
	                                                    <div class="clearafter">
	                                                        <div class="left">
	                                                            <label>
	                                                                <input type="radio" name="payment" checked="checked"/>
	                                                                <strong>Thanh toán trực tiếp</strong>
	                                                            </label>
	                                                        </div>
	                                                        <div class="right">
	                                                            &nbsp;(Nhân viên giao hàng thu tiền)
	                                                        </div>
	                                                    </div>
	                                                </div>
	                                                <div class="right">
	                                                    <h2>Chọn hình thức giao hàng</h2>
	                                                    <div>Giao hàng miễn phí</div>
	                                                </div>
	                                            <br><br><br><br>	                                            	                                            
	                                            <div class="content-center">
	                                            	 <button type="submit" class="button blue">Tiếp tục</button>                                          
	                                            </div>
	                                            </form>
	                                        </div>
	                                    </div>
	                                    <%
	                                		}else if(validStep3!=null&&validStep3.equals("1")){
	                                    %>
	                                    <div class="infoValidated">
	                                    	<div class="clearafter left">
	                                            <div class="left first">Hình thức thanh toán:</div>
	                                            <div class="left last"><strong>Thanh toán trực tiếp</strong></div>
	                                        </div>
	                                        <div class="clearafter right">
	                                        	<div class="left first">Hình thức giao hàng:</div>
	                                            <div class="left last"><strong>Giao hàng miễn phí</strong></div>
	                                        </div>
	                                        <div class="clearbreak"></div>
	                                        <div class="changeInfo"><a href="/thanh-toan?act=doi-thong-tin-thanh-toan" class="changeInfo">Thay đổi</a></div>
	                                    </div>
	                                    <%
	                                		}
	                                    %>
	                                </div>                          		                               
                                <%
                                	}else{
                                		%>
                               		<div class="step-3">
	                                	<h2>3. Thanh toán và vận chuyển</h3>
	                                </div>
                                		<%
                                	}
                                %>
                            
                            
                               	<%
                                	if(validStep1!=null&&validStep1.equals("1")&&validStep2!=null&&validStep2.equals("1")&&validStep3!=null&&validStep3.equals("1")){
                                %> 	
                                	<div class="step-4 active">
                                	<h2>4. Kiểm tra và đặt hàng</h2>
                                	<%
                                		if(order!=null){											
											DealModel deal = DealData.getById(order.getDealId());
											List<String> listStringProduct = new ArrayList<String>();//order.getListProduct();
											NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
                                	%>
                                    <div class="stepContent">
                                    	<form method="post" action="/thanh-toan">
                                    	<input type="hidden" name="action" value="finish"/>
                                    	<p class="sku">Code: <%=order.getOrderId() %></p>
                                            <table cellpadding="0" cellspacing="0">
                                                <thead>
                                                    <tr>
                                                        <th colspan="2">Deal có trong đơn hàng</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                	<%
                                                		if(listStringProduct!=null&&listStringProduct.size()>0){
                                                			for(String stringProduct : listStringProduct){
                                                				OrderProductModel orderItem = new OrderProductModel();
                                                				orderItem.getValueFromString(stringProduct);
                                                	%>
                                                    <tr>
                                                        <td width="40%">
                                                            <a href="/<%=deal.getDealId()%>/<%=deal.getAlias()%>.html"><%=orderItem.getName() %></a>
                                                            
                                                        </td>
                                                        <td width="25%" align="center">
                                                            <strong><%=orderItem.getQuantity() %> x <%=nf.format(order.getUnitPrice()) %> VNĐ = <span class="redColor"><%=nf.format(orderItem.getQuantity()*order.getUnitPrice()) %> VNĐ</span></strong>
                                                        </td>
                                                    </tr>
                                                    <%
                                                			}
                                                		}
                                                    %>                                                    
                                                </tbody>
                                                                                               
                                            </table>
                                            <div class="totalMoney clearafter right">
                                            	<div class="left">Tổng số tiền:</div>
                                                <div class="right redColor"><%=nf.format(order.getTotalPrice()) %> VNĐ</div>
                                            </div>
                                            <div class="clearbreak"></div>
                                            <p>Nếu bạn có thông tin Hotdeal lưu ý, xin hãy nhập nó vào đây:</p>
                                            <textarea name="note"></textarea>
                                            <div class="content-center"><button type="submit" class="button blue">Đặt hàng</button>
                                            </div>
                                        </form>
                                    </div>
                                    <%
                                    }
                                	%>
                                	</div>
                                <%
                                	}else{
                                %> 
                                <div class="step-4">
                                	<h2>4. Kiểm tra và đặt hàng</h2>
                                </div>
                                <%
                                	}
                                %>
                            </div>                                                        
                        </div>
                    </div>
                    <div class="right rightSideBar">
                    	<jsp:include page="/elements/right_menu.jsp"/>
                    </div>
                </div>
            </div>  
<jsp:include page="/layouts/footer.jsp">
    <jsp:param name="language" value="1" />
</jsp:include>